package xmlproject;

import java.io.File;

/**
 *
 * @author Yayo
 */
public class FileAndDirectorySizeUtils {

    private static Long size = 0l;

    public static Long getAttributeSize() {
        return size;
    }

    public static void setAttributeSize(Long value) {
        size = value;
    }

    /**
     * Returns a file size in Bytes, ,kiloBytes, megaBytes or gigaBytes
     *
     * @param file a file instance
     * @return finalSize
     */
    public static String getSize(File file) {

        Long size = file.length();
        if (getFiguresCount(size) <= 3) {
            return String.valueOf(size) + " B";
        } else {
            if (getFiguresCount(size) >= 4 && getFiguresCount(size) <= 6) {
                double finalSize = (double) size / 1024;
                return String.valueOf(Math.round(finalSize * 1000.0) / 1000.0 + " kB");
            } else {
                if (getFiguresCount(size) >= 7 && getFiguresCount(size) <= 9) {
                    double finalSize = (double) size / (1024 * 1024);
                    return String.valueOf(Math.round(finalSize * 1000.0) / 1000.0) + " mB";
                } else {
                    if (getFiguresCount(size) >= 10 && getFiguresCount(size) <= 12) {
                        double finalSize = (double) size / (1024 * 1024 * 1024);
                        return String.valueOf(Math.round(finalSize * 1000.0) / 1000.0) + " gB";
                    }
                }
            }
        }
        return "";
    }

    /**
     * Returns a file size in Bytes, ,kiloBytes, megaBytes or gigaBytes
     *
     * @param size an Long attribute responsive to size of file
     * @return finalSize
     */
    public static String getSize(Long size) {

        if (getFiguresCount(size) <= 3) {
            return String.valueOf(size) + "B";
        } else {
            if (getFiguresCount(size) >= 4 && getFiguresCount(size) <= 6) {
                double finalSize = (double) size / 1024;
                return String.valueOf(Math.round(finalSize * 1000.0) / 1000.0 + "kB");
            } else {
                if (getFiguresCount(size) >= 7 && getFiguresCount(size) <= 9) {
                    double finalSize = (double) size / (1024 * 1024);
                    return String.valueOf(Math.round(finalSize * 1000.0) / 1000.0) + "mB";
                } else {
                    if (getFiguresCount(size) >= 10 && getFiguresCount(size) <= 12) {
                        double finalSize = (double) size / (1024 * 1024 * 1024);
                        return String.valueOf(Math.round(finalSize * 1000.0) / 1000.0) + "gB";
                    }
                }
            }
        }
        return "";
    }

    /**
     * Returns count of figures in given number
     *
     * @param number
     * @return figureCount
     */
    public static int getFiguresCount(Long number) {

        int figureCount = 0;

        while (number != 0) {
            figureCount++;
            number = number / 10;
        }
        return figureCount;
    }

    /**
     * Gets through a file system from given directory and adds size of everyone
     * file to variable 'size'
     *
     * @param files an array of childs of a given directory
     */
    public static void getDirectorySize(File[] files) {

        for (File file : files) {
            if (file.isDirectory()) {
                getDirectorySize(file.listFiles());
            } else {
                size += file.length();
            }
        }
    }

    public static boolean isXML(File file) {

        String suffix = file.getName();
        int i = suffix.lastIndexOf('.');

        if (i > 0 && i < suffix.length() - 1) {
            suffix = suffix.substring(i + 1).toLowerCase();
            if (suffix.compareTo("xml") == 0) {
                return true;
            }
            return false;
        }
        return false;
    }

    public static boolean checkIfFileExists(String filename) {
        
        File dir = new File(".");
        for (File file : dir.listFiles()) {
            if (file.isFile() && file.getName().equals(filename)) {
                return true;
            }
        }
        return false;
    }
}
